import type { DropdownItemOption } from 'vant';
import type { GameId, SearchMapItem, SearchSettingList } from '/@/api/deckSet/model/setDeckModel';
import { awaitWrap } from '/@/utils';
import { DeckConfigApi } from '/@/api/currentDeck/deck';
import { reactive } from 'vue';

// 列表搜索配置
const searchConfigData = reactive<SearchSettingList>([]);
// 属性显示配置
const showConfigData = reactive<SearchSettingList>([]);

const getDeckConfig = (params: GameId) => {
  const handleSearchMapping = (mapping: SearchMapItem[]) => {
    const resultMapping: DropdownItemOption[] = [];
    for (const mapItem of mapping) {
      resultMapping.push({
        value: mapItem.key,
        text: mapItem.value,
      });
    }
    return resultMapping;
  };
  const deckConfig = async () => {
    const [error, result] = await awaitWrap(DeckConfigApi({ game_id: params.game_id }));
    if (error || !result) {
      return;
    }
    searchConfigData.length = 0;
    showConfigData.length = 0;

    for (const searchItem of result.search_setting) {
      const resultMapping = handleSearchMapping(searchItem.mapping || []);
      searchConfigData.push({
        title: searchItem.title,
        attr_key: searchItem.attr_key,
        show_type: searchItem.show_type,
        is_mapping: 0,
        mapping: resultMapping,
      });
    }
    const tagsMappings: any[] = [];
    if (result.tags) {
      for (const tag of result.tags) {
        tagsMappings.push({
          value: tag,
          text: tag,
        });
      }
    }
    searchConfigData.push({
      title: '标签',
      attr_key: 'tags',
      show_type: 'text' as any,
      is_mapping: 0,
      mapping: tagsMappings,
    });

    for (const showItem of result.show_setting) {
      const mappings = handleSearchMapping(showItem.mapping || []);
      showConfigData.push({
        title: showItem.title,
        attr_key: showItem.attr_key,
        show_type: showItem.show_type,
        is_mapping: showItem.is_mapping,
        mapping: mappings,
      });
    }

    console.log(searchConfigData);
    console.log(showConfigData);
  };
  deckConfig();
};

export const useSetDeckConfigHooks = () => {
  return {
    searchConfigData,
    showConfigData,
    getDeckConfig,
  };
};
